<%--
  Created by IntelliJ IDEA.
  User: cos
  Date: 2022/6/10
  Time: 10:06
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="common/head.jsp"%>
<html>

<head>
    <title>Title</title>

    <style>
        body {
            background-image: url("static/img/Java学科壁纸-windows.png");
        }

        #loginForm {
            margin-top: 200px;
            border: 1px solid gainsboro;
            padding: 25px;
            border-radius: 15px; /* 圆角 */
            background-color: white;
            opacity: 0.9; /* 透明度 */
        }
    </style>
</head>
<body>
<div class="container">
    <div class="col-md-4 col-md-offset-4">
        <form id="loginForm">
            <div class="form-group">
                <label for="inputEname">用户名</label>
                <input type="email" class="form-control" name="ename" id="inputEname" placeholder="Username">
                <span></span>
            </div>
            <div class="form-group">
                <label for="inputPwd">密码</label>
                <input type="password" class="form-control" name="pwd" id="inputPwd"
                       placeholder="Password">
            </div>
            <button type="button" id="subBtn" class="btn btn-default" disabled>Submit</button>
        </form>
    </div>
</div>
<script>
    $(function () {
        $("#inputEname").change(function () {
            //发送ajax请求 ,提供用户名是否存在
            $.ajax({
                type: "get",
                url: "/emp/check/" + this.value,
                success: function (result) {
                    if (result.code == 1) {
                        $("#inputEname").next().text(result.msg).css("color", "green");
                        // 把提交按钮的禁用解除
                        $("#subBtn").prop("disabled", false);
                    } else {
                        $("#inputEname").next().text(result.msg).css("color", "red");
                        // 设置提交按钮为禁用
                        $("#subBtn").prop("disabled", true);
                    }
                }
            })
        })


        $("#subBtn").click(function () {
            let loginData = $("#loginForm").serialize();
            //发送ajax进行登录处理
            $.ajax({
                type: "post",
                url: "/emp/login",
                data: loginData,
                success: function (result) {
                    if (result.code == 1) {
                        // alert(result.msg);
                        //登录成功进行页面跳转
                        location.href="/index.jsp";
                    } else {
                        alert(result.msg);
                        //清空已有输入框的数据
                        $("#inputEname").val("");
                        //情况提示信息
                        $("#inputEname").next().text("");
                        $("#inputPwd").val("");
                    }
                }
            })
        })
    })
</script>
</body>
</html>
